package com.colorfruit.admin.control.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.colorfruit.admin.business.service.ConfigService;
import com.colorfruit.admin.business.service.ShopHomeService;
import com.colorfruit.admin.pojo.constant.RedisConstant;
import com.colorfruit.admin.pojo.enums.ErrorCodeEnum;
import com.colorfruit.admin.pojo.enums.StatusEnum;
import com.colorfruit.admin.pojo.vo.preonshelf.PreOnShelfVo;
import com.colorfruit.admin.pojo.vo.preonshelf.PreOnShelfVoTotal;
import com.colorfruit.framework.common.util.DataValidateUtil;
import com.colorfruit.framework.model.pojo.BaseResult;
import com.colorfruit.framework.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shopHome")
public class ShopHomeController {

    @Autowired
    ShopHomeService shopHomeService;

    @Autowired
    ConfigService configService;

    @Autowired
    RedisCache redisCache;

    /**
     * 查询左侧banner
     * @return
     */
    @GetMapping("/getLeftBanner")
    public BaseResult getLeftBanner(){
        try {
            log.info("# [IN] [controller] getLeftBanner param:");
            String homeLeftBanner = null;
            Integer redisStatus = configService.getRedisStatus();
            if (redisStatus == StatusEnum.EFFECTIVE_STATUS.getStatusCode()){//判断是否开启redis缓存
                homeLeftBanner = redisCache.getCache(RedisConstant.HOME_LEFT_BANNER, String.class);
            }
            if (!DataValidateUtil.verifyString(homeLeftBanner)){
                homeLeftBanner = shopHomeService.getLeftBanner();
            }
            log.info("# [OUT] [controller] getLeftBanner SUCCESS");
            return new BaseResult(homeLeftBanner);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 查询活动产品
     * @return
     */
    @GetMapping("/selectActivityProductList")
    public BaseResult selectActivityProductList(){
        try {
            log.info("# [IN] [controller] getActivityProductList param:");
            String homeActivityStr = null;
            Integer redisStatus = configService.getRedisStatus();
            if (redisStatus == StatusEnum.EFFECTIVE_STATUS.getStatusCode()){//判断是否开启redis缓存
                homeActivityStr = redisCache.getCache(RedisConstant.HOME_ACTIVITY_LIST, String.class);
            }
            List<PreOnShelfVo> preOnShelfVos = JSON.parseObject(homeActivityStr,new TypeReference<List<PreOnShelfVo>>(){});
            if (preOnShelfVos == null || preOnShelfVos.size() == 0){
                preOnShelfVos = shopHomeService.selectActivityProductList();
            }
            log.info("# [OUT] [controller] getActivityProductList SUCCESS");
            return new BaseResult(preOnShelfVos);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 查询新品
     * @return
     */
    @GetMapping("/selectNewArrivalProductList")
    public BaseResult selectNewArrivalProductList(){
        try {
            log.info("# [IN] [controller] getActivityProductList param:");
            String homNewArrivalStr = null;
            Integer redisStatus = configService.getRedisStatus();
            if (redisStatus == StatusEnum.EFFECTIVE_STATUS.getStatusCode()){ //判断是否开启redis缓存
                homNewArrivalStr = redisCache.getCache(RedisConstant.HOME_NEW_ARRIVAL_LIST, String.class);
            }
            List<PreOnShelfVo> preOnShelfVos = JSON.parseObject(homNewArrivalStr ,new TypeReference<List<PreOnShelfVo>>(){});
            if (preOnShelfVos == null || preOnShelfVos.size() == 0){
                preOnShelfVos = shopHomeService.selectNewArrivalProductList();
            }
            log.info("# [OUT] [controller] getActivityProductList SUCCESS");
            return new BaseResult(preOnShelfVos);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 查询热销
     * @return
     */
    @GetMapping("/selectHotSaleProductList")
    public BaseResult selectHotSaleProductList(){
        try {
            log.info("# [IN] [controller] getActivityProductList param:");
            String homNewArrivalStr = null;
            Integer redisStatus = configService.getRedisStatus();
            if (redisStatus == StatusEnum.EFFECTIVE_STATUS.getStatusCode()){//判断是否开启redis缓存
                homNewArrivalStr = redisCache.getCache(RedisConstant.HOME_HOT_SALE_LIST, String.class);
            }
            List<PreOnShelfVo> preOnShelfVos = JSON.parseObject(homNewArrivalStr ,new TypeReference<List<PreOnShelfVo>>(){});
            if (preOnShelfVos == null || preOnShelfVos.size() == 0){
                preOnShelfVos = shopHomeService.selectHotSaleProductList();
            }
            log.info("# [OUT] [controller] getActivityProductList SUCCESS");
            return new BaseResult(preOnShelfVos);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 查询普通产品
     * @param productType
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/selectHomeNormalProductList")
    public BaseResult selectHomeNormalProductList(String productType, int page, int size){
        try {
            log.info("# [IN] [controller] selectHomeNormalProductList param: productType = {}, page = {}, size = {}", productType, page, size);
            String homeNormalProductList = null;
            Integer redisStatus = configService.getRedisStatus();
            if (redisStatus == StatusEnum.EFFECTIVE_STATUS.getStatusCode()){//判断是否开启redis缓存
                homeNormalProductList = redisCache.getCache(RedisConstant.HOME_NORMAL_PRODUCT_LIST + productType + page + size, String.class);
            }
            List<PreOnShelfVoTotal> preOnShelfVos = JSON.parseObject(homeNormalProductList ,new TypeReference<List<PreOnShelfVoTotal>>(){});
            if (preOnShelfVos == null || preOnShelfVos.size() == 0){
                preOnShelfVos = shopHomeService.selectHomeNormalProductList(productType, page, size);
            }
            log.info("# [OUT] [controller] selectHomeNormalProductList SUCCESS");
            return new BaseResult(preOnShelfVos);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }



}
